﻿//*****************************字符串对象的增强方法************************************

String.prototype.xmlEncode = function () {
    /// <summary>Xml特殊字符处理：<转化成&lt; &转化成&amp;</summary>
    return this.replace(/&/g, "&amp;").replace(/</g, "&lt;");
};

String.prototype.xmlDecode = function () {
    /// <summary>Xml特殊字符处理：&lt;转化成< &amp;转化成&</summary>
    return this.replace(/&amp;/g, "&").replace(/&lt;/g, "<");
};

String.prototype.trim = function () {
    /// <summary>去掉字符串前后的空格</summary>
    return this.replace(/(^[\s]*)|([\s]*$)/g, "");
};

String.prototype.leftTrim = function () {
    /// <summary>去掉字符串左边的空格</summary>
    return this.replace(/(^[\s]*)/g, "");
};

String.prototype.rightTrim = function () {
    /// <summary>去掉字符串右边的空格</summary>
    return this.replace(/([\s]*$)/g, "");
};

String.prototype.urlAppend = function (sString) {
    /// <summary>追加url参数</summary>
    /// <param name="sString" type="String">需要追加的url参数</param>
    var sUrl = this;
    return sUrl + (sUrl.indexOf('?') === -1 ? '?' : '&') + sString;
};

String.prototype.left = function (iCount) {
    /// <summary>截取字符串左边的iCount个字符</summary>
    /// <param name="iCount" type="Int">需要截取的字符长度</param>
    return this.substr(0, iCount);
};
	
String.prototype.right = function (iCount) {
    /// <summary>截取字符串右边的iCount个字符</summary>
    /// <param name="iCount" type="Int">需要截取的字符长度</param>
    return this.substr(this.length - iCount, iCount);
};

String.prototype.startWith = function (sSubStr, bIgnoreCase) {
    /// <summary>判断字符串是否以某个字段开头</summary>
    /// <param name="sSubStr" type="String">被判断的子字符串</param>
    /// <param name="bIgnoreCase" type="Boolean">是否区分大小写(true:不区分; false:区分)</param>
    if (this.length < sSubStr.length)
        return false;

    var sTemp = this.substr(0, sSubStr.length);
    if (bIgnoreCase) {
        if (sTemp.toLowerCase() == sSubStr.toLowerCase())
            return true;
    }
    else {
        if (sTemp == sSubStr)
            return true;
    }
    return false;
};

String.prototype.endWith = function (sSubStr, bIgnoreCase) {
    /// <summary>判断字符串是否以某个字段结尾</summary>
    /// <param name="sSubStr" type="String">被判断的子字符串</param>
    /// <param name="bIgnoreCase" type="Boolean">是否区分大小写(true:不区分; false:区分)</param>
    if (this.length < sSubStr.length)
        return false;

    var sTemp = this.substr(this.length - sSubStr.length, sSubStr.length);
    if (bIgnoreCase) {
        if (sTemp.toLowerCase() == sSubStr.toLowerCase())
            return true;
    }
    else {
        if (sTemp == sSubStr)
            return true;
    }
    return false;
};

String.prototype.leftOf = function (sSubStr) {
    /// <summary>取字符串中sSubStr以左的部分(如果字符串中有多个sSubStr, 则指的是第一个)</summary>
    /// <param name="sSubStr" type="String">标志字符串</param>
    if (this.indexOf(sSubStr) == -1)
        return "";
    else
        return this.substr(0, this.indexOf(sSubStr));
};

String.prototype.leftOfLast = function (sSubStr) {
    /// <summary>取字符串中sSubStr以左的部分(如果字符串中有多个sSubStr, 则指的是最后一个)</summary>
    /// <param name="sSubStr" type="String">标志字符串</param>
    if (this.indexOf(sSubStr) == -1)
        return "";
    else
        return this.substr(0, this.lastIndexOf(sSubStr));
};

String.prototype.rightOf = function (sSubStr) {
    /// <summary>取字符串中sSubStr以右的部分(如果字符串中有多个sSubStr, 则指的是第一个)</summary>
    /// <param name="sSubStr" type="String">标志字符串</param>
    if (this.indexOf(sSubStr) == -1)
        return "";
    else
        return this.substr(this.indexOf(sSubStr) + sSubStr.length);
};

String.prototype.rightOfLast = function (sSubStr) {
    /// <summary>取字符串中sSubStr以右的部分(如果字符串中有多个sSubStr, 则指的是最后一个)</summary>
    /// <param name="sSubStr" type="String">标志字符串</param>
    if (this.indexOf(sSubStr) == -1)
        return "";
    else
        return this.substr(this.lastIndexOf(sSubStr) + sSubStr.length);
};

String.prototype.contains = function (sSubStr, sDiv, bIgnoreCase) {
    /// <summary>比较子字符串是否存在, 可指定是否区分大小写，并且还可以判断是否需要分隔符</summary>
    /// <param name="sSubStr" type="String">需要查找的子字符串</param>
    /// <param name="sDiv" type="String">字符串的分隔符</param>
    /// <param name="bIgnoreCase" type="Boolean">是否区分大小写(true:不区分; false:区分)</param>
    if (sDiv) {
        var sStr = sDiv + this + sDiv;
        sSubStr = sDiv + sSubStr + sDiv;

        //大小写不敏感
        if (bIgnoreCase) {
            return sStr.toLowerCase().indexOf(sSubStr.toLowerCase()) >= 0;
        }
        else {
            //大小写敏感
            return sStr.indexOf(sSubStr) >= 0;
        }
    }
    else {
        return this.indexOf(sSubStr) >= 0;
    }
};

//escape不编码字符有69个：[*] [+] [-] [.] [/] [@] [_] [0-9] [a-z] [A-Z]
//encodeURI不编码字符有82个：[!] [#] [$] [&] ['] [(] [)] [*] [+] [,] [-] [.] [/] [:] [;] [=] [?] [@] [_] [~] [0-9] [a-z] [A-Z]
//encodeURIComponent不编码字符有71个：[!] ['] [(] [)] [*] [-] [.] [_] [~] [0-9] [a-z] [A-Z]
//String.prototype.escape = function () {
//    return escape(this);
//};

String.prototype.format = function () {
    /// <summary>模拟.NET的格式化字符串</summary>
    var sMe = this;
    if (arguments && arguments.length > 0) {
        for (var i = 0, j = arguments.length; i < j; i++) {
            var oRegex = new RegExp("\\{" + i + "\\}", "gm");
            sMe = sMe.replace(oRegex, arguments[i]);
        }
    }
    return sMe;
};

String.prototype.replaceAll = function (sStrA, sStrB) {
    /// <summary>将字符串中所有的sStrA替换成sStrB</summary>
    if (!sStrA) return this;
    var sReturn = this;
    while (sReturn.indexOf(sStrA) != -1) {
        sReturn = sReturn.replace(sStrA, sStrB);
    }
    return sReturn;
};

String.prototype.hasChineseChar = function () {
    /// <summary>判断字符串中是否有汉字</summary>
    if (/[^\x00-\xff]/g.test(this))
        return true;
    else
        return false;
};

String.prototype.lengthB = function () {
    /// <summary>获取字符串的真实宽度（多字节符(汉字、全角符等)，按2个字符计算）</summary>
    return this.replace(/[^x00-xff]/g, "**").length;
};

String.prototype.append = function (sSubStr, sDivChar) {
    /// <summary>往字符串中追加内容</summary>
    /// <param name="sSubStr" type="String">追加的子字符串</param>
    /// <param name="sDivStr" type="String">原内容与追加内容之间的分隔符号</param>
    if (!sDivChar) sDivChar = ";";
    if (!this)
        return sSubStr;
    else
        return this + sDivChar + sSubStr;
};

String.prototype.removeSubString = function (sSubStr, sDivChar) {
    /// <summary>从字符串中删除指定的内容</summary>
    /// <param name="sSubStr" type="String">要删除的子字符串</param>
    /// <param name="sDivStr" type="String">源字符串中各个子字符串之间的分隔符号</param>
    if (!sDivChar) sDivChar = ";";
    var sReturn = sDivChar + this + sDivChar;
    sReturn = sReturn.replaceAll(sDivChar + sSubStr + sDivChar, sDivChar);
    while (sReturn.indexOf(sDivChar + sDivChar) != -1) {
        sReturn = sReturn.ReplaceAll(sDivChar + sDivChar, sDivChar);
    }
    if (sReturn.startWith(sDivChar)) {
        sReturn = sReturn.substr(sDivChar.length);
    }
    if (sReturn.endWith(sDivChar)) {
        sReturn = sReturn.substr(0, sReturn.length - sDivChar.length);
    }
    return sReturn;
};

String.prototype.removeEmptySubString = function (sDivChar) {
    /// <summary>删除空的子字符串</summary>
    /// <param name="sDivStr" type="String">源字符串中各个子字符串之间的分隔符号</param>
    if (!sDivChar) sDivChar = ";";
    var sReturn = "";
    var aSubStrs = this.split(sDivChar);
    for (var i = 0; i < aSubStrs.length; i++) {
        if (aSubStrs[i] == "") continue;
        sReturn = sReturn.append(aSubStrs[i], sDivChar);
    }
    return sReturn;
};

//
String.prototype.removeRepeatSubString = function (sDivChar) {
    /// <summary>删除重复的子字符串</summary>
    /// <param name="sDivStr" type="String">源字符串中各个子字符串之间的分隔符号</param>
    if (!sDivChar) sDivChar = ";";
    var sReturn = "";
    var aSubStrs = this.split(sDivChar);
    for (var i = 0; i < aSubStrs.length; i++) {
        if (sReturn.hasSubString(aSubStrs[i], sDivChar) == false)
            sReturn = sReturn.append(aSubStrs[i], sDivChar);
    }
    return sReturn;
};

String.prototype.hasSubString = function (sSubStr, sDivStr, bIgnoreCase) {
    /// <summary>比较子字符串是否存在, 可指定是否区分大小写</summary>
    /// <param name="sDivStr" type="String">源字符串中各个子字符串之间的分隔符号</param>
    /// <param name="bIgnoreCase" type="Boolean">是否区分大小写(true:不区分; false:区分)</param>
    var sStr = sDiv + this + sDiv;
    sSubStr = sDiv + sSubStr + sDiv;

    if (bIgnoreCase) {
        //大小写不敏感
        return sStr.toLowerCase().has(sSubStr.toLowerCase());
    }
    else {
        //大小写敏感
        return sStr.has(sSubStr);
    }
};

String.prototype.has = function (sSubStr) {
    /// <summary>判断本字符串中是否含有指定的子字符串</summary>
    /// <param name="sSubStr" type="String">需要查找的子字符串</param>
    if (this.indexOf(sSubStr) == -1)
        return false;
    else
        return true;
};
